# Maintainer: Ignacio Casal Quinteiro <icq@gnome.org>

_realname=libgusb
_tarballname=gusb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.3.3
pkgrel=1
arch=('any')
pkgdesc="GLib wrapper around libusb1 (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection")
options=('strip' 'staticlibs')
license=("LGPL")
url="https://github.com/hughsie/libgusb"
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/hughsie/${_realname}/archive/${pkgver}.tar.gz"
        fix-build.patch
        introspection-optional.patch)
sha256sums=('fcb338573609e18a0de07b32c40325f03c0c4632f906848ae7ec7da355c4cf1f'
            'a3f23ea5e84d6c369492526670d38f0fb77d57a9e93df20d143d079dc6c2cbb8'
            'c94f3481c5a6addb0088a3cdb4cc01ea539ec2c68325f51170e25b73c7aee987')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/fix-build.patch
  patch -p1 -i ${srcdir}/introspection-optional.patch
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  CFLAGS+=" -fstack-protector"
  meson \
    --buildtype plain \
    --prefix=${MINGW_PREFIX} \
    -Dintrospection=true \
    ../${_realname}-${pkgver}

  ninja
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR=${pkgdir}${MINGW_PREFIX} ninja install
  
  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PRE_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
